<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="generator" content="hevea 2.18">
<link rel="stylesheet" type="text/css" href="manual.css">
<title>Chapter&#XA0;31&#XA0;&#XA0;The threads library</title>
</head>
<body>
<a href="libstr.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="libgraph.html"><img src="next_motif.gif" alt="Next"></a>
<hr>
<h1 class="chapter" id="sec557">Chapter&#XA0;31&#XA0;&#XA0;The threads library</h1>
<p>
<a id="c:threads"></a></p><p>The <span class="c003">threads</span> library allows concurrent programming in OCaml.
It provides multiple threads of control (also called lightweight
processes) that execute concurrently in the same memory space. Threads
communicate by in-place modification of shared data structures, or by
sending and receiving data on communication channels.</p><p>The <span class="c003">threads</span> library is implemented by time-sharing on a single
processor. It will not take advantage of multi-processor machines.
Using this library will therefore never make programs run
faster. However, many programs are easier to write when structured as
several communicating processes.</p><p>Two implementations of the <span class="c003">threads</span> library are available, depending
on the capabilities of the operating system:
</p><ul class="itemize"><li class="li-itemize">
System threads. This implementation builds on the OS-provided threads
facilities: POSIX 1003.1c threads for Unix, and Win32 threads for
Windows. When available, system threads support both bytecode and
native-code programs.
</li><li class="li-itemize">VM-level threads. This implementation performs time-sharing and
context switching at the level of the OCaml virtual machine (bytecode
interpreter). It is available on Unix systems, and supports only
bytecode programs. It cannot be used with native-code programs.
</li></ul><p>
Programs that use system threads must be linked as follows:
</p><pre>
        ocamlc -thread <span class="c009">other options</span> unix.cma threads.cma <span class="c009">other files</span>
        ocamlopt -thread <span class="c009">other options</span> unix.cmxa threads.cmxa <span class="c009">other files</span>
</pre><p>
Compilation units that use the <span class="c003">threads</span> library must also be compiled with
the <span class="c003">-thread</span> option (see chapter&#XA0;<a href="comp.html#c%3Acamlc">9</a>).</p><p>Programs that use VM-level threads must be compiled with the <span class="c003">-vmthread</span>
option to <span class="c003">ocamlc</span> (see chapter&#XA0;<a href="comp.html#c%3Acamlc">9</a>), and be linked as follows:
</p><pre>
        ocamlc -vmthread <span class="c009">other options</span> threads.cma <span class="c009">other files</span>
</pre><p>
Compilation units that use <span class="c003">threads</span> library must also be compiled with
the <span class="c003">-vmthread</span> option (see chapter&#XA0;<a href="comp.html#c%3Acamlc">9</a>).</p><ul class="ftoc2"><li class="li-links">
<a href="libref/Thread.html">Module <span class="c003">Thread</span>: lightweight threads</a>
</li><li class="li-links"><a href="libref/Mutex.html">Module <span class="c003">Mutex</span>: locks for mutual exclusion</a>
</li><li class="li-links"><a href="libref/Condition.html">Module <span class="c003">Condition</span>: condition variables to synchronize between threads</a>
</li><li class="li-links"><a href="libref/Event.html">Module <span class="c003">Event</span>: first-class synchronous communication</a>
</li><li class="li-links"><a href="libref/ThreadUnix.html">Module <span class="c003">ThreadUnix</span>: thread-compatible system calls</a>
</li></ul>
<hr>
<a href="libstr.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="libgraph.html"><img src="next_motif.gif" alt="Next"></a>
</body>
</html>
